package com.zeta.eduservice.utils.word;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.BreakType;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
import org.springframework.stereotype.Component;

import com.zeta.eduservice.entity.question.EduQuestionChoice;
import com.zeta.eduservice.entity.vo.front.ChapterCourseDetialVo;
import com.zeta.eduservice.entity.vo.front.PdfQuestionVo;
import com.zeta.eduservice.entity.vo.front.QuestionCreateInfo;
import com.zeta.eduservice.entity.vo.front.front.PaperInfoVo;
@Component
public class WordCreateUtils {
	

	

	/////////////////////////////////////////////////////////////////////////////////
	public void choiceTable(XWPFDocument document, QuestionCreateInfo info, int size) {
		int t = 1;
		if (size % 10 > 0) {
			t = size / 10;
			t++;
		}
		if (size > 10) {
			size = 10;
		}
		XWPFTable table = document.createTable(2 * t - 1, size);
		table.createRow();
		List<XWPFTableRow> rows = table.getRows();
		// 表格属性
		CTTblPr tablePr = table.getCTTbl().addNewTblPr();
		// 表格宽度
		CTTblWidth width = tablePr.addNewTblW();
		width.setW(BigInteger.valueOf(8000));
		XWPFTableRow row;
		List<XWPFTableCell> cells;
		XWPFTableCell cell;
		int rowSize = rows.size();
		int max = info.getNumber();
		int num = 1;
		for (int i = 0; i < rowSize; i++) {

			row = rows.get(i);
			// 新增单元格
			row.addNewTableCell();
			// 设置行的高度
			row.setHeight(500);
			// 行属性
//	       CTTrPr rowPr = row.getCtRow().addNewTrPr();  
			// 这种方式是可以获取到新增的cell的。
//	       List<CTTc> list = row.getCtRow().getTcList();  
			cells = row.getTableCells();
			for (int j = 0; j < size + 1; j++) {

				cell = cells.get(j);
//	                cell.setColor("ff0000"); //红色  
				// 单元格属性
				if (i % 2 == 0) {
					if (j == 0)
						cell.setText("题号");
					else if (j > 0 && max >= num) {
						cell.setText(num++ + "");
					}
				} else {
					if (j == 0) {
						cell.setText("答案");
					} else if (j > 0) {
						cell.setText(" ");
					}
				}
				CTTc cttc = cell.getCTTc();
				CTTcPr ctPr = cttc.addNewTcPr();
				ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
				ctPr.addNewTcW().setW(BigInteger.valueOf(800));
				//cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
			}
		}
	}
	public void choiceTable(XWPFDocument document,int size) {
		int t = 1;
		if (size % 10 > 0) {
			t = size / 10;
			t++;
		}
		if (size > 10) {
			size = 10;
		}
		XWPFTable table = document.createTable(2 * t - 1, size);
		table.createRow();
		List<XWPFTableRow> rows = table.getRows();
		// 表格属性
		CTTblPr tablePr = table.getCTTbl().addNewTblPr();
		// 表格宽度
		CTTblWidth width = tablePr.addNewTblW();
		width.setW(BigInteger.valueOf(8000));
		XWPFTableRow row;
		List<XWPFTableCell> cells;
		XWPFTableCell cell;
		int rowSize = rows.size();
		
		int num = 1;
		for (int i = 0; i < rowSize; i++) {

			row = rows.get(i);
			// 新增单元格
			row.addNewTableCell();
			// 设置行的高度
			row.setHeight(500);
			// 行属性
//	       CTTrPr rowPr = row.getCtRow().addNewTrPr();  
			// 这种方式是可以获取到新增的cell的。
//	       List<CTTc> list = row.getCtRow().getTcList();  
			cells = row.getTableCells();
			for (int j = 0; j < size + 1; j++) {

				cell = cells.get(j);
//	                cell.setColor("ff0000"); //红色  
				// 单元格属性
				if (i % 2 == 0) {
					if (j == 0)
						cell.setText("题号");
					else if (j > 0 && size >= num) {
						cell.setText(num++ + "");
					}
				} else {
					if (j == 0) {
						cell.setText("答案");
					} else if (j > 0) {
						cell.setText(" ");
					}
				}
				CTTc cttc = cell.getCTTc();
				CTTcPr ctPr = cttc.addNewTcPr();
				ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
				ctPr.addNewTcW().setW(BigInteger.valueOf(800));
				//cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
			}
		}
	}

	public void mergeCellsHorizontal(XWPFTable table, int row, int startCell, int endCell) {
		for (int i = startCell; i <= endCell; i++) {
			XWPFTableCell cell = table.getRow(row).getCell(i);
			if (i == startCell) {
				// The first merged cell is set with RESTART merge value
				cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
			} else {
				// Cells which join (merge) the first one, are set with CONTINUE
				cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
			}
		}
	}

	/**
	 * word单元格行合并
	 * 
	 * @param table   表格
	 * @param col     合并行所在列
	 * @param fromRow 开始行
	 * @param toRow   结束行
	 * @date 2020年4月8日 下午4:46:18
	 */
	public static void mergeCellsVertically(XWPFTable table, int col, int startRow, int endRow) {
		for (int i = startRow; i <= endRow; i++) {
			XWPFTableCell cell = table.getRow(i).getCell(col);
			if (i == startRow) {
				// The first merged cell is set with RESTART merge value
				cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
			} else {
				// Cells which join (merge) the first one, are set with CONTINUE
				cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
			}
		}
	}
    /** 
     * @Description: 页面Break 
     */  
    public void addNewPageBreak(XWPFDocument document, BreakType breakType) {  
        XWPFParagraph xp = document.createParagraph();  
        xp.createRun().addBreak(breakType);  
    }  
    public  void searchAndReplace(String srcPath, String destPath,Map<String, String> map) throws IOException {
       
            XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(srcPath));
            /**
             * 替换段落中的指定文字
             */
            Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
            while (itPara.hasNext()) {
                XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
                Set<String> set = map.keySet();
                Iterator<String> iterator = set.iterator();
                while (iterator.hasNext()) {
                    String key = iterator.next();
                    List<XWPFRun> run=paragraph.getRuns();
                    for(int i=0;i<run.size();i++)
                    {
                        if(run.get(i).getText(run.get(i).getTextPosition())!=null &&
                                run.get(i).getText(run.get(i).getTextPosition()).equals(key))
                        {
                            /**
                             * 参数0表示生成的文字是要从哪一个地方开始放置,设置文字从位置0开始
                             * 就可以把原来的文字全部替换掉了
                             */
                            run.get(i).setText(map.get(key),0);
                        }
                    }
                }
            
        
    }
    
    }
}
